/**
 * 
 */
package com.swim.phonenumprivacy.service;

import java.time.format.DateTimeFormatter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.aliyun.dyplsapi20170525.Client;
import com.aliyun.dyplsapi20170525.models.BindAxbRequest;
import com.aliyun.dyplsapi20170525.models.BindAxbResponse;
import com.aliyun.dyplsapi20170525.models.UnbindSubscriptionRequest;
import com.aliyun.dyplsapi20170525.models.UnbindSubscriptionResponse;
import com.aliyun.dyplsapi20170525.models.BindAxbResponseBody.BindAxbResponseBodySecretBindDTO;
import com.aliyun.tea.utils.StringUtils;
import com.swim.phonenumprivacy.entity.AxnDTO;
import com.swim.phonenumprivacy.entity.AxnVO;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class BindAxnExtensionService {

    @Value("${aly.POOL_KEY_AXN}")
    private String POOL_KEY_AXN;

    @Autowired
    private Client client;

    private Logger logger = LoggerFactory.getLogger(BindAxnExtensionService.class);
    
    public AxnVO bindAxnExtension(AxnDTO axnDTO){
//    	BindAxnExtensionRequest request  = new BindAxnExtensionRequest();
        BindAxbRequest request  = new BindAxbRequest();
        request.setPoolKey(POOL_KEY_AXN);
        request.setPhoneNoA(axnDTO.getPhoneA());
        if(!StringUtils.isEmpty(axnDTO.getPhoneB())){
            request.setPhoneNoB(axnDTO.getPhoneB());
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if(axnDTO.getExpiration()!=null) {
            request.setExpiration(axnDTO.getExpiration().format(formatter));
        }
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
//        BindAxnExtensionResponse response = null;
        BindAxbResponse response = null;
        try {
//            response = client.bindAxnExtensionWithOptions(request,runtime);
            response = client.bindAxb(request);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[阿里云添加AXN号码的绑定关系失败 ={}]", e.getMessage());
        }
//        if((response.statusCode).equals(200)){
//            BindAxnExtensionResponseBody.BindAxnExtensionResponseBodySecretBindDTO secretBindDTO = response.getBody().secretBindDTO;
            BindAxbResponseBodySecretBindDTO secretBindDTO = response.getBody().secretBindDTO;

            if (secretBindDTO!=null){
                AxnVO axnVO = new AxnVO();
                axnVO.setSubsId(secretBindDTO.getSubsId());
                axnVO.setSecretNo(secretBindDTO.getSecretNo());
                axnVO.setExtension(secretBindDTO.getExtension());
                axnVO.setPhoneA(axnDTO.getPhoneA());
                axnVO.setPhoneB(axnDTO.getPhoneB());
                axnVO.setExpiration(axnDTO.getExpiration());
                return axnVO;
            }
//        }
        return null;
    }

    public boolean unbindAxnExtension(AxnDTO axnDTO) {
        //组装请求对象
        UnbindSubscriptionRequest request = new UnbindSubscriptionRequest();
        //必填:对应的号池Key
        request.setPoolKey(POOL_KEY_AXN);
        //必填-分配的X小号-对应到绑定接口中返回的secretNo;
        request.setSecretNo(axnDTO.getSecretNo());
        //可选-绑定关系对应的ID-对应到绑定接口中返回的subsId;
        request.setSubsId(axnDTO.getSubsId());
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        UnbindSubscriptionResponse response = null;
        try {
            response = client.unbindSubscriptionWithOptions(request,runtime);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[阿里云添加AXN号码的解绑关系失败 ={}]", e.getMessage());

        }
//        if((response.statusCode).equals(200)){
//            return true;
//        }
        return false;
    }
    
	
//	/**
//	 * 获取绑定关系数据缓存
//	 * @return Map<String, String>  k:macId  v:name
//	 */
//	public Map<String, String> findPunctureDeskNameMap() {
//		Map<String, String> resultMap = new HashMap<String, String>();
//		Object obj = ConcurrentHashMapCacheUtils.getCache("punctureDeskNameMap");
//		if (obj == null) {//缓存为空
//			//设置缓存
//			setPunctureDeskNameMapCache();
//			obj = ConcurrentHashMapCacheUtils.getCache("punctureDeskNameMap");
//		}
//		resultMap = (Map<String, String>) obj;
//		return resultMap;
//	}
//	
//	/**
//	 * 设置绑定关系数据缓存
//	 */
//	public void setPunctureDeskNameMapCache() {
//		Map<String, String> resultMap = new HashMap<String, String>();
//		Device deviceWhere = new Device();
//		deviceWhere.setType(2);		// 类型：1.配药间app 2.穿刺台app 3.输液app
//		List<Device> deviceList = super.findList(deviceWhere);
//		if (CollectionUtils.isNotEmpty(deviceList)) {
//			for (Device device : deviceList) {
//				resultMap.put(device.getMacId(), device.getName());
//			}
//		}
//		ConcurrentHashMapCacheUtils.setCache("punctureDeskNameMap", resultMap);
//	}
}
